/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*   File:      AGV_Send_API_Commands.cpp
*   Author:    刘洋兵
*   Function:  下发AGV相关api指令的界面
*   Time:      2025-09-09
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "AGV_Send_API_Commands.h"
#include "ui_AGV_Send_API_Commands.h"

AGV_Send_API_Commands::AGV_Send_API_Commands(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AGV_Send_API_Commands)
{
    ui->setupUi(this);
}

AGV_Send_API_Commands::~AGV_Send_API_Commands()
{
    delete ui;
}

/**
 * @brief  显示索引指定界面，控件数据初始化
 * @param  table  控件QTableView对象指针
 * @param  index  界面索引
 * @param  function_code  协议功能码
 * @param  tableindex  表格某单元格索引
 */
void AGV_Send_API_Commands::showDisplay_View(QTableView* table,
                                             const int& index,
                                             const int& function_code,
                                             const int& tableindex)
{
    ui->stackedWidget->setCurrentIndex(index);
    _function_code = function_code;

    ui->label_vehicle_name->show();
    ui->comboBox_vehicle_id_map->show();
    ui->label_map->show();
    ui->comboBox_map_name->show();

    QString sql_vehicle = "vehicle_id,vehicle_name";
    QString dbtablename_vehicle = "tb_vehicle_profile";
    QList<QStringList> dataList_vehicle = MyDatabase::getInstance().query_some_tabledata(sql_vehicle,dbtablename_vehicle);
    int rowcount_vehicle = dataList_vehicle.count();

    QString sql_map = "map_code,map_name";
    QString dbtablename_map = "tb_map_config";
    QList<QStringList> dataList_map = MyDatabase::getInstance().query_some_tabledata(sql_map,dbtablename_map);
    int rowcount_map = dataList_map.count();
    if(index == 0)
    {
        setWindowTitle("AGV重定位");
        ui->comboBox_vehicle_id_relocation->clear();
        QStandardItemModel* model = qobject_cast<QStandardItemModel*>(table->model());
        QString vehicle = model->data(model->index(tableindex, 1)).toString();
        int vehicle_id = model->data(model->index(tableindex, 0)).toInt();
        ui->comboBox_vehicle_id_relocation->addItem(vehicle,vehicle_id);

        QString sql = "node_id,node_name";
        QString dbtablename = "tb_nodes";
        QString id = QString("relocate_pos = 1");
        QList<QStringList> dataList = MyDatabase::getInstance().query_some_tabledata(sql,dbtablename,id);
        int rowcount = dataList.count();
        ui->comboBox_relocate_pos->clear();
        for (int row = 0; row < rowcount; row++) {
            if(ui->comboBox_relocate_pos->findText(dataList[row][1]) == -1)
            ui->comboBox_relocate_pos->addItem(dataList[row][1],dataList[row][0]);
        }

    }
    if(index == 1)
    {
        ui->comboBox_vehicle_id_map->clear();
        ui->comboBox_map_name->clear();
        for (int row = 0; row < rowcount_vehicle; row++) {
            ui->comboBox_vehicle_id_map->addItem(dataList_vehicle[row][1],dataList_vehicle[row][0]);
        }
        for (int row = 0; row < rowcount_map; row++) {
            ui->comboBox_map_name->addItem(dataList_map[row][1],dataList_map[row][0]);
        }
        switch (_function_code) {
        case 308:
            setWindowTitle("获取地图站点信息");
            ui->comboBox_map_name->setCurrentIndex(0);
            ui->comboBox_map_name->setEnabled(true);
            ui->comboBox_vehicle_id_map->setCurrentIndex(0);
            ui->comboBox_vehicle_id_map->setEnabled(true);
            break;
        case 311:
            setWindowTitle("获取当前地图pgm文件");
            ui->label_map->hide();
            ui->comboBox_map_name->hide();
            ui->comboBox_map_name->setCurrentIndex(-1);
            ui->comboBox_map_name->setEnabled(false);
            ui->comboBox_vehicle_id_map->setCurrentIndex(0);
            ui->comboBox_vehicle_id_map->setEnabled(true);
            break;
        case 312:
            setWindowTitle("获取当前地图原点坐标");
            ui->label_map->hide();
            ui->comboBox_map_name->hide();
            ui->comboBox_map_name->setCurrentIndex(-1);
            ui->comboBox_map_name->setEnabled(false);
            ui->comboBox_vehicle_id_map->setCurrentIndex(0);
            ui->comboBox_vehicle_id_map->setEnabled(true);
            break;
        case 316:
            setWindowTitle("更新三方地图站点信息");
            ui->label_vehicle_name->hide();
            ui->comboBox_vehicle_id_map->hide();
            ui->comboBox_map_name->setCurrentIndex(0);
            ui->comboBox_map_name->setEnabled(true);
            ui->comboBox_vehicle_id_map->setCurrentIndex(-1);
            ui->comboBox_vehicle_id_map->setEnabled(false);
            break;
        case 317:
            setWindowTitle("查询设置的三方地图站点信息");
            ui->label_map->hide();
            ui->comboBox_map_name->hide();
            ui->comboBox_map_name->setCurrentIndex(-1);
            ui->comboBox_map_name->setEnabled(false);
            ui->comboBox_vehicle_id_map->setCurrentIndex(0);
            ui->comboBox_vehicle_id_map->setEnabled(true);
            break;
        }
    }
    if(index == 2)
    {
        ui->comboBox_vehicle_id_strategy_query->clear();
        ui->comboBox_map_strategy_query->clear();
        for (int row = 0; row < rowcount_vehicle; row++) {
            ui->comboBox_vehicle_id_strategy_query->addItem(dataList_vehicle[row][1],dataList_vehicle[row][0]);
        }
        for (int row = 0; row < rowcount_map; row++) {
            ui->comboBox_map_strategy_query->addItem(dataList_map[row][1],dataList_map[row][0]);
        }
    }
    if(index == 3)
    {
        ui->comboBox_vehicle_id_strategy_alter->clear();
        for (int row = 0; row < rowcount_vehicle; row++) {
            ui->comboBox_vehicle_id_strategy_alter->addItem(dataList_vehicle[row][1],dataList_vehicle[row][0]);
        }
    }
    exec();
}

/**
 * @brief  发送AGV重定位协议
 */
void AGV_Send_API_Commands::on_pushButton_send_relocation_clicked()
{
    //发送数据类
    QMap<QString, QVariant> infomap;
    QMap<QString, QVariant> dataMap;
    infomap["vehicle_id"] = ui->comboBox_vehicle_id_relocation->currentData().toInt();

    QString relocate_pos = ui->comboBox_relocate_pos->currentData().toString();
    QString sql = "node_x,node_y,angle";
    QString dbtablename = "tb_nodes";
    QString id = QString("node_id = '%1'").arg(relocate_pos);
    QList<QStringList> dataList = MyDatabase::getInstance().query_some_tabledata(sql,dbtablename,id);

    dataMap["x"] = dataList[0][0].toDouble();
    dataMap["y"] = dataList[0][1].toDouble();
    dataMap["angle"] = dataList[0][2].toDouble();

    QString vehicle_name = ui->comboBox_vehicle_id_relocation->currentText();
    QString msg = QString("确认小车%1重定位").arg(vehicle_name);
    if(CommandSendorReceive::getInstance().isExecute("提示",msg))
    {
        //打包成json数据结构
        CommandSendorReceive::getInstance().sendCommand(dataMap, infomap, _function_code, true);
        LOG_INFO(msg);
    }
    close();
}

/**
 * @brief  发送map地图相关协议
 */
void AGV_Send_API_Commands::on_pushButton_send_map_clicked()
{
    //发送数据类
    QMap<QString, QVariant> infomap;
    QMap<QString, QVariant> dataMap;
    int vehicle_id = ui->comboBox_vehicle_id_map->currentData().toInt();
    QString map_code = ui->comboBox_map_name->currentData().toString();
    infomap["vehicle_id"] = vehicle_id;
    infomap["map_code"] = map_code;

    if(CommandSendorReceive::getInstance().isExecute("提示","确认发送map相关协议"))
    {
        //打包成json数据结构
        CommandSendorReceive::getInstance().sendCommand(dataMap, infomap, _function_code, false);
        LOG_INFO("确认发送map相关协议");
    }
    close();
}

/**
 * @brief  发送AGV避障策略查询协议
 */
void AGV_Send_API_Commands::on_pushButton_send_strategy_query_clicked()
{
    close();
}

/**
 * @brief  发送AGV避障策略修改协议
 */
void AGV_Send_API_Commands::on_pushButton_send_strategy_alter_clicked()
{
    close();
}
